﻿


#include "httpserver.h"


#include <QTcpServer>

QHash<int, QString> STATUS_CODES;

HttpServer::HttpServer(QObject *parent)
    : QObject(parent)
    , m_tcpServer(0)
{
#define STATUS_CODE(num, reason) STATUS_CODES.insert(num, reason);
// {{{
  STATUS_CODE(100, "Continue")
  STATUS_CODE(101, "Switching Protocols")
  STATUS_CODE(102, "Processing")                 // RFC 2518) obsoleted by RFC 4918
  STATUS_CODE(200, "OK")
  STATUS_CODE(201, "Created")
  STATUS_CODE(202, "Accepted")
  STATUS_CODE(203, "Non-Authoritative Information")
  STATUS_CODE(204, "No Content")
  STATUS_CODE(205, "Reset Content")
  STATUS_CODE(206, "Partial Content")
  STATUS_CODE(207, "Multi-Status")               // RFC 4918
  STATUS_CODE(300, "Multiple Choices")
  STATUS_CODE(301, "Moved Permanently")
  STATUS_CODE(302, "Moved Temporarily")
  STATUS_CODE(303, "See Other")
  STATUS_CODE(304, "Not Modified")
  STATUS_CODE(305, "Use Proxy")
  STATUS_CODE(307, "Temporary Redirect")
  STATUS_CODE(400, "Bad Request")
  STATUS_CODE(401, "Unauthorized")
  STATUS_CODE(402, "Payment Required")
  STATUS_CODE(403, "Forbidden")
  STATUS_CODE(404, "Not Found")
  STATUS_CODE(405, "Method Not Allowed")
  STATUS_CODE(406, "Not Acceptable")
  STATUS_CODE(407, "Proxy Authentication Required")
  STATUS_CODE(408, "Request Time-out")
  STATUS_CODE(409, "Conflict")
  STATUS_CODE(410, "Gone")
  STATUS_CODE(411, "Length Required")
  STATUS_CODE(412, "Precondition Failed")
  STATUS_CODE(413, "Request Entity Too Large")
  STATUS_CODE(414, "Request-URI Too Large")
  STATUS_CODE(415, "Unsupported Media Type")
  STATUS_CODE(416, "Requested Range Not Satisfiable")
  STATUS_CODE(417, "Expectation Failed")
  STATUS_CODE(418, "I\"m a teapot")              // RFC 2324
  STATUS_CODE(422, "Unprocessable Entity")       // RFC 4918
  STATUS_CODE(423, "Locked")                     // RFC 4918
  STATUS_CODE(424, "Failed Dependency")          // RFC 4918
  STATUS_CODE(425, "Unordered Collection")       // RFC 4918
  STATUS_CODE(426, "Upgrade Required")           // RFC 2817
  STATUS_CODE(500, "Internal Server Error")
  STATUS_CODE(501, "Not Implemented")
  STATUS_CODE(502, "Bad Gateway")
  STATUS_CODE(503, "Service Unavailable")
  STATUS_CODE(504, "Gateway Time-out")
  STATUS_CODE(505, "HTTP Version not supported")
  STATUS_CODE(506, "Variant Also Negotiates")    // RFC 2295
  STATUS_CODE(507, "Insufficient Storage")       // RFC 4918
  STATUS_CODE(509, "Bandwidth Limit Exceeded")
  STATUS_CODE(510, "Not Extended")                // RFC 2774
// }}}

    qRegisterMetaType<ReplyJson >("ReplyJson");
    qRegisterMetaType<SAPExcelInfo >("SAPExcelInfo");
    qRegisterMetaType<QMap<QString,AGVStatus>>("QMap<QString, AGVStatus>");
    qRegisterMetaType<QMap<QString,AGVTaskStatus>>("QMap<QString, AGVTaskStatus>");
    qRegisterMetaType<QMap<QString,SAPExcelInfo>>("QMap<QString, SAPExcelInfo>");

    //容器写入数据
//    Map_write();

    _agvStaustTask = new sapMsgInterfaceI;

    threadQueryI=new threadQuery;
    threadQueryItem=new QThread();
    threadQueryI->moveToThread(threadQueryItem);
    threadQueryItem->start();

    StTimer = new QTimer(this);
    connect(StTimer,&QTimer::timeout,this,&HttpServer::ON_HttpServerTimer);
    StTimer->start(100);
}

HttpServer::~HttpServer()
{
}

void HttpServer::newConnection()
{
    Q_ASSERT(m_tcpServer);
    while(m_tcpServer->hasPendingConnections()) {
        HttpConnection *connection = new HttpConnection(m_tcpServer->nextPendingConnection(), this);
        connect(connection,&HttpConnection::newReplyData_task,this,&HttpServer::ON_newReplyData_task);
        connect(connection, SIGNAL(newRequest(HttpRequest*, HttpResponse*)),
		   this, SLOT(onRequest(HttpRequest*, HttpResponse*)));
    }
}

bool HttpServer::listen(const QHostAddress &address, quint16 port)
{
    port_=port;
    m_tcpServer = new QTcpServer;
    connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
    return m_tcpServer->listen(address, port);
}

bool HttpServer::listen(quint16 port)
{
    return listen(QHostAddress::Any, port);
}

bool HttpServer::close_()
{
    m_tcpServer->close();
    m_tcpServer->deleteLater();
    this->deleteLater();
    return true;
}

void HttpServer::ON_AgvTaskBool(int TaskBool)
{
    _agvTaskhttpBool=TaskBool;                                      //任务状态--:  0：启动 1：暂停
    qDebug()<<"手动更改接口启动、暂停状态-->agvTaskhttpBool:"<<_agvTaskhttpBool;
}

//总响应
void HttpServer::onRequest(HttpRequest* req, HttpResponse* resp)
{
	//实现你的响应代码
	QString reply  = tr("<html><head><title>Rokh Server Test</title></head><body><h1>hello!</h1></body></html>");
	resp->setHeader("Content-Type", "text/html");
	resp->setStatus(200);

    //http 接口数据解析返回
    QJsonObject postJson=_ReplyJson(currentReplyJsonI.taskType,currentReplyJsonI);
    resp->jsend_(postJson);

//    QJsonArray rootFruitList;
//    rootFruitList.append(postJson);
//    resp->jsend(rootFruitList);

	return;

}

//新建AGV任务
void HttpServer::ON_HttpServerTimer()
{

    ProcesskeyId_agv();                                //获取 AGV 状态信息任务

    stTimercount++;

    switch (stTimercount%remainder) //
    {
        case 0x00:
//            if(!ReplyJson_agvStatus.isEmpty()){
//                    qDebug()<<"ReplyJson_agvStatus:"<<ReplyJson_agvStatus; //agv状态不为空打印出来
//            }
//            currentStationInfoMap = threadQueryI->select_StationMap();//实时查询库位状态信息
            break;
        case 0x01:

            break;
        case 0x03:

            break;
        default:
            break;
    }


    if(stTimercount>100000){stTimercount=0;}

}

//新的请求反馈数据
void HttpServer::ON_newReplyData_task(QString ClientIP,ReplyJson ReplyJsonI,QJsonObject ReplyJson)
{
    currentReplyJsonI=ReplyJsonI;

//    currentStationInfoMap = threadQueryI->select_StationMap();//实时查询库位状态信息

//    if(ReplyJsonI.taskType=="newTask"){
//        currentReplyJsonI=analysReplyJson_new(ReplyJson,currentReplyJsonI);// 平板 通过 POST 方式创建订单任务
//    }

//    if(ReplyJsonI.taskType=="taskStatus"){
//        currentReplyJsonI=analysReplyJson_taskStatus_MES(ReplyJson,currentReplyJsonI);// 平板 通过 POST 方式获取任务状态信息
//    }

//    if(ReplyJsonI.taskType.contains("StationStatus?lang=")){        //PDA 通过 POST 方式获取库位状态信息
//        currentReplyJsonI=analysReplyJson_StationStatus_MES(ReplyJson, currentReplyJsonI, ReplyJsonI.taskType);
//    }

//    if(ReplyJsonI.taskType=="runTask" || ReplyJsonI.taskType=="agvRun"){
//        currentReplyJsonI=analysReplyJson_runTask(ReplyJson,currentReplyJsonI);// 平板放行 异常删除任务
//    }

//    if(ReplyJsonI.taskType=="deviceStatus"){        //平板 获取库位PLC信息
//        currentReplyJsonI=analysReplyJson_StationPLC_MES(ReplyJson,currentReplyJsonI);
//    }

//    if(ReplyJsonI.taskType=="alterdeviceStatus"){   //平板 更改库位信息
//        currentReplyJsonI=analysReplyJson_alterdeviceStatus(ReplyJson,currentReplyJsonI);
//    }

//    if(ReplyJsonI.taskType=="statusAgv"){
//        currentReplyJsonI=analysReplyJson_statusAgv(ReplyJson,currentReplyJsonI);// 平板 查询AGV 状态信息
//    }

//    if(ReplyJsonI.taskType=="agvStatus"){          //AGV状态反馈
//        currentReplyJsonI=analysReplyJson_agvStatus(ReplyJson,currentReplyJsonI);
//    }

    if(ReplyJsonI.taskType=="agvTaskStatus"){      //AGV任务状态反馈
        currentReplyJsonI=analysReplyJson_taskStatus(ReplyJson,currentReplyJsonI);
    }

//    if(ReplyJsonI.taskType=="stationStatus?stationId="){   //摄像头查询库位状态
//        currentReplyJsonI=analysReplyJson_CameraStatus_MES(ReplyJson,currentReplyJsonI);
//    }

    HttpOnLineTimer=QDateTime::currentDateTime();
}

//创建AGV任务
ReplyJson HttpServer::analysReplyJson_new(QJsonObject ReplyJson_,ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson = ReplyJson_;

    // 获取 josn 数据
    SAPExcelInfo SAPExcelInfoI;
    SAPExcelInfoI.taskId = ReplyJson.value("taskId").toString();                    // 任务编码
    SAPExcelInfoI.taskType = ReplyJson.value("taskType").toString();                // 任务类型
    SAPExcelInfoI.agvNum = ReplyJson.value("agvId").toString();                     // 小车编号
    SAPExcelInfoI.load = ReplyJson.value("load").toString();                        // 起始库位
    SAPExcelInfoI.unload = ReplyJson.value("unload").toString();                    // 目标库位
    SAPExcelInfoI.creatTimer = QDateTime::currentDateTime();                        // 创建时间

    ReplyJsonI.status = 100;

    if (ReplyJsonI.status == 100){

        // 起始库位判断
        if (SAPExcelInfoI.load != "" && ServerInitItem.StationInfoMap.contains(SAPExcelInfoI.load)){

            deviceI deviceItem = ServerInitItem.StationInfoMap.find(SAPExcelInfoI.load).value();
            SAPExcelInfoI.loadPoint = deviceItem.currentStation;                    // 起始库位站点 

        }else{
            ReplyJsonI.status = 1;// 接料工位异常
        }

        // 目标库位判断
        if (SAPExcelInfoI.unload != "" && ServerInitItem.StationInfoMap.contains(SAPExcelInfoI.unload)){

            deviceI deviceItem = ServerInitItem.StationInfoMap.find(SAPExcelInfoI.unload).value();
            SAPExcelInfoI.unloadPoint = deviceItem.currentStation;                    // 起始库位站点

        }else{
            ReplyJsonI.status = 2;// 对接工位异常
        }

        // 任务类型
        if (SAPExcelInfoI.taskType == "ONE"){
            SAPExcelInfoI.taskTypeDesc = "101固废转运";
        }else if (SAPExcelInfoI.taskType == "TWO"){
            SAPExcelInfoI.taskTypeDesc = "102进料转运";
        }else if (SAPExcelInfoI.taskType == "THREE"){
            SAPExcelInfoI.taskTypeDesc = "102固废转运";
        }else if (SAPExcelInfoI.taskType == "FOUR"){
            SAPExcelInfoI.taskTypeDesc = "102成品转运";
        }

        // 实时获取当前任务
        newSAPExcelInfoList = ESS_Single::GetInstance()->getCurrentSAPExcelInfoTask();
        foreach (SAPExcelInfo iter,newSAPExcelInfoList.values()){

            if (iter.taskStatus < 100 && iter.load == SAPExcelInfoI.load && iter.unload == SAPExcelInfoI.unload){

                ReplyJsonI.status = 3;// 重复下发任务

                qDebug()<<"该任务重复下发--> taskType:"<<SAPExcelInfoI.taskType<<" taskId:"<<SAPExcelInfoI.taskId
                       <<" load:"<<SAPExcelInfoI.load<<" unload:"<<SAPExcelInfoI.unload;
            }
        }

        if (ReplyJsonI.status == 100){

            ReplyJsonI.status = 0;
            SAPExcelInfoI.taskStatus = 0;
            SAPExcelInfoI.latstaskStatus = 0;
            QString taskType = SAPExcelInfoI.taskType + "_" + QString::number(SAPExcelInfoI.taskStatus);
            SAPExcelInfoI.taskStatusDesc = ServerInitItem.taskStatusMap.value(taskType);

            emit sig_SAPExcelInfo_new(SAPExcelInfoI);// new task
        }

    }

    ReplyJsonI.errorMsg = ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);

    qDebug()<<"-------MES 创建任务 -----> taskType:"<<SAPExcelInfoI.taskType<<" taskTypeDesc:"<<SAPExcelInfoI.taskTypeDesc
           <<" scanCode:"<<SAPExcelInfoI.taskId_agv<<" taskId:"<<SAPExcelInfoI.taskId<<" load:"<<SAPExcelInfoI.load
             <<" status:"<<ReplyJsonI.status<<" errorMsg:"<<ReplyJsonI.errorMsg;

    return ReplyJsonI;
}

 //PDA 通过 POST 方式获取任务状态信息
ReplyJson HttpServer::analysReplyJson_taskStatus_MES(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson=ReplyJson_;

    QMap<int, SAPExcelInfo> CurrentSAPExcelInfoTaskMap= ESS_Single::GetInstance()->getCurrentSAPExcelInfoTask();

    ReplyJsonI.status=0;
    ReplyJsonI.tasksJson=SAP_transFor_Object("MES",CurrentSAPExcelInfoTaskMap);

//    if(!CurrentSAPExcelInfoTaskMap.isEmpty()){
//        ReplyJsonI.status=0;
//        ReplyJsonI.tasksJson=SAP_transFor_Object("MES",CurrentSAPExcelInfoTaskMap);
//    }else{
//        ReplyJsonI.status=1;//任务编码异常
//        ReplyJsonI.tasksJson.insert("status",2);
//        ReplyJsonI.tasksJson.insert("message","任务链表为空");
//    }

    qDebug()<<"获取任务状态信息 ReplyJsonI.tasksJson:"<<ReplyJsonI.tasksJson;

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;
}

ReplyJson HttpServer::analysReplyJson_StationStatus_MES(QJsonObject ReplyJson_, ReplyJson ReplyJsonI, QString language)
{
    QJsonObject ReplyJson=ReplyJson_;

    QJsonObject ReJson;
    QJsonArray rootFruitList;

    QMap<QString,deviceI>::iterator iter;
    foreach(deviceI iter,currentStationInfoMap.values()){

        if(iter.stationDesc == "C1" || iter.stationDesc == "DD1" || iter.stationDesc == "DDW1"
                || iter.stationDesc == "DD2" || iter.stationDesc == "DDW2"
                || iter.stationDesc == "WC1" || iter.stationDesc == "WCW1"
                || iter.stationDesc == "WC2" || iter.stationDesc == "WCW2"
                || iter.stationDesc == "S1" || iter.stationDesc == "S2")
        {
            continue;
        }

        QJsonObject tasksJsonI;
        tasksJsonI.insert("StationName", iter.deviceDesc);                                                          //库位名称

        if(language == "StationStatus?lang=vi")
        {
//            tasksJsonI.insert("StationNameDesc", ESS_Single::GetInstance()->_StationNameDescMap_VI.value(iter.deviceDesc));//库位名称描述
//            tasksJsonI.insert("StationStatusDesc", ESS_Single::GetInstance()->_StationStatusDescMap_VI.value(iter.stationStatus));//库位状态描述
        }else
        {
            tasksJsonI.insert("StationNameDesc", ESS_Single::GetInstance()->_StationNameDescMap.value(iter.deviceDesc));//库位名称描述
            tasksJsonI.insert("StationStatusDesc", ESS_Single::GetInstance()->_StationStatusDescMap.value(iter.stationStatus));//库位状态描述
        }

        tasksJsonI.insert("StationStatus", iter.stationStatus);                                                     //库位状态

        rootFruitList.append(tasksJsonI);
    }

    ReJson.insert("taskType", "MES");      //类型识别号 AGV MES
    ReJson.insert("ArrayType", rootFruitList);

    ReplyJsonI.status=0;
    ReplyJsonI.tasksJson=ReJson;

//    qDebug()<<"获取库位状态信息 ReplyJsonI.tasksJson:"<<ReplyJsonI.tasksJson<<" taskType: MES";
    qDebug()<<"--------获取库位状态信息-------> taskType: MES";

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;
}


//MES 通过 GET 摄像头方式获取库位状态
ReplyJson HttpServer::analysReplyJson_CameraStatus_MES(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson=ReplyJson_;
    //QString taskId=ReplyJson.value("taskId").toString();     //任务订单号，必须是唯一

    //QString dateTime_str = dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    // 从字符串转换为毫秒（需完整的年月日时分秒）
    //datetime.fromString("2011-09-10 12:07:50:541", "yyyy-MM-dd hh:mm:ss:zzz").toMSecsSinceEpoch();
    QDateTime  UpDateTime;
    QString StationName;                                       //站点名称
    quint64 GetStatusTime;                                     //获取站点状态的时间戳
    int stationId;                                             //站点编号
    int stationStatus;                                         //站点状态


    stationId = ReplyJson.value("stationId").toInt();          //站点编号
    stationStatus = ReplyJson.value("stationId").toInt();      //站点状态   -1出错   0空位    1空架    2满架
    //GetStatusTime = ReplyJson.value("updateTime").s;


    // 从字符串转换为毫秒（需完整的年月日时分秒）
    //datetime.fromString("2011-09-10 12:07:50:541", "yyyy-MM-dd hh:mm:ss:zzz").toMSecsSinceEpoch();
    if(stationId > 0 && stationId < 12)
    {
        StationName = QString("N%1").arg(stationId);  //站点名称拼接,北车间
    }else
        StationName = QString("S%1").arg(stationId);  //站点名称拼接,北车间

    ReplyJsonI.StationStatusMap.insert(StationName,stationStatus);  //插入摄像头识别的库位状态列表
    ReplyJsonI.getStatusTime = GetStatusTime;                       //获取站点状态的时间戳


    qDebug()<<"获取库位状态时间 taskId:"<<ReplyJsonI.getStatusTime<<ReplyJsonI.tasksJson;

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;
}

ReplyJson HttpServer::analysReplyJson_StationPLC_MES(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson=ReplyJson_;
    QString taskId=ReplyJson.value("taskId").toString();         //任务订单号，必须是唯一

    ReplyJsonI.tasksJson.insert("taskId",taskId);                //订单编码唯一
    if(taskId!=""){
        ModbusAll _ModbusAll= ESS_Single::GetInstance()->getModbusAllData();
        ReplyJsonI.status=0;//接收成功
        ReplyJsonI.tasksJson = PLC_transFor_Object("PLC", _ModbusAll);
    }else{
        ReplyJsonI.status=-1;//非法值错误
        ReplyJsonI.tasksJson.insert("status", 1);
        ReplyJsonI.tasksJson.insert("message", "订单编码错误");
    }

    qDebug()<<"analysReplyJson_StationPLC_MES --->taskId:"<<taskId<<ReplyJsonI.tasksJson;

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->_errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;
}

//任务状态赋予
QJsonObject HttpServer::SAP_transFor_Object(QString taskType, QMap<int, SAPExcelInfo> currentSAPExcelInfoTask)
{
    QJsonObject ReJson;
    QJsonArray rootFruitList;
    QMap<int, SAPExcelInfo>::iterator iter= currentSAPExcelInfoTask.begin();
    while (iter!=currentSAPExcelInfoTask.end()) {
        qint64 statusTimers =0;
        if(!iter.value().finishTimer.isNull()){
            statusTimers= iter.value().finishTimer.secsTo(QDateTime::currentDateTime());
        }
        if(iter.value().taskStatus < taskFnish){

            QJsonObject tasksJson;
            tasksJson.insert("taskId",iter.value().taskId);                                             // 任务编码 唯一性
            tasksJson.insert("agvId",iter.value().agvNum);                                              // 小车编号
            tasksJson.insert("taskType",iter.value().taskType);                                         // 任务类型
            tasksJson.insert("taskTypeDesc",iter.value().taskTypeDesc);                                 // 任务类型描述
            tasksJson.insert("SAPTaskIndex",iter.value().SAPTaskIndex);                                 // 任务序列号 唯一值
            tasksJson.insert("taskStatus",iter.value().taskStatus);                                     // 任务状态
            tasksJson.insert("taskStatusDesc",iter.value().taskStatusDesc);                             // 任务状态注释
            tasksJson.insert("load",iter.value().load);                                                 // 起始库位
            tasksJson.insert("unload",iter.value().unload);                                             // 目标库位
            tasksJson.insert("creatTimer",iter.value().creatTimer.toString("yyyy-MM-dd hh:mm:ss"));     // 创建时间

            rootFruitList.append(tasksJson);
        }

//        qDebug()<<taskType<<" SAPExcelInfoTask--> 样品Id:"<<iter.value().material_ID<<" 存储位置:"<<iter.value().storage_Site<<iter.value().storage_Area
//              <<" taskStatus:"<<iter.value().taskStatus <<" 数量:"<<iter.value().storageReal_Qty;

        iter++;
    }

    ReJson.insert("taskType",taskType);      //类型识别号 AGV MES
    ReJson.insert("ArrayType",rootFruitList);

    qDebug()<<"ReplyJson-->taskType:"<<taskType<<" ReJson:"<<ReJson;
    return  ReJson;
}

QJsonObject HttpServer::PLC_transFor_Object(QString taskType, ModbusAll ModbusAllI)
{
    QJsonObject ReJson;
    QJsonArray rootFruitList;

    QJsonObject tasksJson;
//    tasksJson.insert("IX10", ModbusAllI.I.X10);// 碘线对接位 进料任务
//    tasksJson.insert("IX12", ModbusAllI.I.X12);// 碘线对接位 出料任务

//    tasksJson.insert("LuX10", ModbusAllI.Lu.X10);// 镥线对接位 进料任务
//    tasksJson.insert("LuX12", ModbusAllI.Lu.X12);// 镥线对接位 出料任务

//    tasksJson.insert("SrX10", ModbusAllI.Sr.X10);// 锶线对接位 进料任务
//    tasksJson.insert("SrX12", ModbusAllI.Sr.X12);// 锶线对接位 出料任务

    rootFruitList.append(tasksJson);

    ReJson.insert("taskType",taskType);      //类型识别号 AGV MES
    ReJson.insert("ArrayType",rootFruitList);

    qDebug()<<"ReplyJson-->taskType:"<<taskType<<" ReJson:"<<ReJson;
    return  ReJson;
}

//MES 平板放行
ReplyJson HttpServer::analysReplyJson_runTask(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson = ReplyJson_;
    RunTaskData RunTaskDataI;
    QString taskId = ReplyJson.value("taskId").toString();                  // 任务订单号，必须是唯一
    RunTaskDataI.status = ReplyJson.value("status").toInt();                // 放行标志
    RunTaskDataI.RunStation = ReplyJson.value("RunStation").toString();     // 放行位置，防止后续库位

    SAPExcelInfo SAPExcelInfoI;
    SAPExcelInfoI.taskId=taskId;
    if(RunTaskDataI.status<100){
        ReplyJsonI=checkTaskRepeat_run(SAPExcelInfoI,ReplyJsonI);    //判断当前任务是否可以放行
        if(ReplyJsonI.status==0){
            ReplyJsonI.errorMsg="放行成功";
            ESS_Single::GetInstance()->setRunTaskMap(taskId,RunTaskDataI);    //设置平板放行链表
        }else{
            if(ESS_Single::GetInstance()->_language == "VI")
            {
//                ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
            }else
            {
                ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);
            }
        }
    }else{
        ReplyJsonI.status=0;
        ReplyJsonI.errorMsg="删除成功";
        ESS_Single::GetInstance()->setRunTaskMap(taskId,RunTaskDataI);       //设置平板放行链表
    }


    qDebug()<<"MES 下达放行指令--->taskId:"<<taskId<<" status:"<<RunTaskDataI.status<<" RunStation:"<<RunTaskDataI.RunStation
           <<" errorMsg:"<<ReplyJsonI.errorMsg<<" ReplyJson:"<<ReplyJson_;
    return ReplyJsonI;
}

ReplyJson HttpServer::analysReplyJson_alterdeviceStatus(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson=ReplyJson_;
    QJsonObject ReJson;
    QJsonArray rootFruitList=ReplyJson.value("statusList").toArray();
    QJsonValue value = ReplyJson.value("statusList");//第一级对象 的值
    if (value.isObject()){  //判读第二级对象是否存在
        ReJson=value.toObject();
        qDebug() <<"ReJson statusList:"<<ReJson;
    }if(value.isArray()){//判读第二级对象是以数组形式存在
        ReplyJsonI.status=100;
        ESS_Single::GetInstance()->setStationStatusMAP(value, rootFruitList);
    }

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);
    }

    qDebug()<<"analysReplyJson_alterdeviceStatus--> status:"<<ReplyJsonI.status<<" size():"<<rootFruitList.size()<<" ReplyJson:"<<ReplyJson;
    return ReplyJsonI;
}

//平板查询AGV状态
ReplyJson HttpServer::analysReplyJson_statusAgv(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{

    QJsonObject ReplyJson=ReplyJson_;
    QString taskId=ReplyJson.value("taskId").toString();         //任务订单号，必须是唯一
    ReplyJsonI.tasksJson.insert("taskId",taskId);                //订单编码唯一
    if(taskId!=""){
        QMap<QString, AGVCurrentTask> AGVCurrentTaskMap_= ESS_Single::GetInstance()->getAGVCurrentTaskMap();
        if(!AGVCurrentTaskMap_.isEmpty()){
            ReplyJsonI.status=0;
            ReplyJsonI.tasksJson=AGV_transFor_Object("AGV",AGVCurrentTaskMap_);
        }else{
            ReplyJsonI.status=1;//任务编码异常
            ReplyJsonI.tasksJson.insert("status",2);
            ReplyJsonI.tasksJson.insert("message","任务链表为空");
        }
    }else{
        ReplyJsonI.status=1;//任务编码异常
        ReplyJsonI.tasksJson.insert("status",1);
        ReplyJsonI.tasksJson.insert("message","订单编码错误");
    }

    qDebug()<<"statusAgv --->taskId:"<<taskId<<ReplyJsonI.tasksJson;

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;

}

//AGV状态赋予
QJsonObject HttpServer::AGV_transFor_Object(QString taskType, QMap<QString, AGVCurrentTask> AGVCurrentTaskMap_)
{
    QJsonObject ReJson;
    QJsonArray rootFruitList;
    QMap<QString, AGVCurrentTask>::iterator iter= AGVCurrentTaskMap_.begin();
    while (iter!=AGVCurrentTaskMap_.end()) {
        QJsonObject tasksJson;
        tasksJson.insert("AGVId",iter.value().AGVId);                   // 小车编号
        tasksJson.insert("AGVIP",iter.value().AGVIP);                   // 小车 ip
        tasksJson.insert("taskId",iter.value().taskId);                 // 任务 ID
        tasksJson.insert("unitId",iter.value().unitId);                 // 子任务 ID
        tasksJson.insert("taskType",iter.value().taskType);             // 任务类型
        tasksJson.insert("status",iter.value().status);                 // 任务状态
        tasksJson.insert("SAPTaskIndex",iter.value().SAPTaskIndex);     // 序列号
        tasksJson.insert("station",iter.value().station);               // 动作站点
        tasksJson.insert("currentStation",iter.value().currentStation); // 当前站点
        tasksJson.insert("AGVState",iter.value().AGVState);             // AGV 状态
        tasksJson.insert("batteryLevel",iter.value().batteryLevel);     // AGV 电量
        tasksJson.insert("inputIOStatus",iter.value().inputIOStatus);   // 输入IO状态
        tasksJson.insert("errorCode",iter.value().errorCode);           // 故障代码

        QString errorCode="";
        if(!iter.value().isOnline){
            errorCode="离线";
        }else{
            if(iter.value().errorCode!=0){
               errorCode="错误:"+QString::number(iter.value().errorCode);
            }if(iter.value().batteryLevel<30){
                 errorCode="低电量";
            }
        }
        tasksJson.insert("errorCode",errorCode);
        rootFruitList.append(tasksJson);

        iter++;
    }

    ReJson.insert("taskType",taskType);      //类型识别号
    ReJson.insert("ArrayType",rootFruitList);

    qDebug()<<"ReplyJson-->taskType:"<<taskType<<" ReJson:"<<ReJson;

    return  ReJson;
}

//返回AGV状态信息
ReplyJson HttpServer::analysReplyJson_agvStatus(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    ReplyJsonI.status=-1;
    QJsonObject ReplyJson=ReplyJson_;
    QJsonObject ReJson;
    QJsonArray rootFruitList=ReplyJson.value("agvList").toArray();
    QJsonValue value = ReplyJson.value("agvList");//第一级对象 的值
    if (value.isObject()){  //判读第二级对象是否存在
        ReJson=value.toObject();
        qDebug() <<"ReJson agvList:"<<ReJson;
    }if(value.isArray()){//判读第二级对象是以数组形式存在
        for(int i=0;i<rootFruitList.size();i++){
            ReJson=value[i].toObject();
            AGVStatus AGVStatusI;
            AGVStatusI.agvId=ReJson.value("agvId").toInt();                         //agv 编号
            AGVStatusI.taskId=ReJson.value("taskId").toString();                    //任务 ID，未执行任务可为空
            AGVStatusI.unitId=ReJson.value("unitId").toString();                    //子任务 ID，未执行任务可为空
            AGVStatusI.state=ReJson.value("state").toInt();                         //AGV 状态 0：正在运行，1：空闲， 2：异常，3：正在充电
            AGVStatusI.errorCode=ReJson.value("errorCode").toInt();                 //故障代码（故障代码参考表）
            AGVStatusI.alarmCode=ReJson.value("alarmCode").toInt();                 //警告代码（警告代码参考表）
            AGVStatusI.batteryLevel=ReJson.value("batteryLevel").toInt();           //AGV 电量，百分比
            AGVStatusI.currentNode=ReJson.value("currentNode").toInt();             //当前的节点
            AGVStatusI.lastNodeId=ReJson.value("lastNodeId").toInt();               //上次的节点
            AGVStatusI.currentStation=ReJson.value("currentStation").toInt();       //当前的站点
            AGVStatusI.lastNodeStation=ReJson.value("lastNodeStation").toInt();     //上次节点所属的站点
            AGVStatusI.agvPosition=ReJson.value("agvPosition").toString();          //AGV位置信息(x,y)---(6090,20310)
            AGVStatusI.inputIOStatus=ReJson.value("inputIOStatus").toInt();         //输入IO状态（32个状态位）
            AGVStatusI.outputIOStatus=ReJson.value("outputIOStatus").toInt();       //输出IO状态（32个状态位）

            ReplyJsonI.status=0;
            if(AGVStatusI.agvId>0){
                if(AGVStatusMap.contains(QString::number(AGVStatusI.agvId))){
                    AGVStatusMap.find(QString::number(AGVStatusI.agvId)).value()=AGVStatusI;
                }else{
                    AGVStatusMap.insert(QString::number(AGVStatusI.agvId),AGVStatusI);
                }
            }

            qDebug()<<"AGVStatus--->agvId:"<<AGVStatusI.agvId<<" taskId:"<<AGVStatusI.taskId<<" state:"<<AGVStatusI.AGVState<<" errorCode:"<<AGVStatusI.errorCode
                 <<" alarmCode:"<<AGVStatusI.alarmCode<<" batteryLevel"<<AGVStatusI.batteryLevel<<" currentStation:"<<AGVStatusI.currentStation
                 <<" agvPosition:"<<AGVStatusI.agvPosition;
        }

        emit sig_AGVStatusWidget(AGVStatusMap,AGVTaskStatusMap);

    }

    ReplyJson_agvStatus=ReplyJson;

    //qDebug()<<rootFruitList.size()<<"--analysReplyJson_agvStatus--> ReplyJson:"<<ReplyJson;

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->_errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->_errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;
}

//返回任务状态
ReplyJson HttpServer::analysReplyJson_taskStatus(QJsonObject ReplyJson_, ReplyJson ReplyJsonI)
{
    QJsonObject ReplyJson=ReplyJson_;
    AGVTaskStatus AGVTaskStatusI;
    AGVTaskStatusI.taskId=ReplyJson.value("taskId").toString();     //任务 ID
    AGVTaskStatusI.unitId=ReplyJson.value("unitId").toString();     //子任务 ID
    AGVTaskStatusI.status=ReplyJson.value("status").toInt();        //任务状态   0:未执行 1:正在运行, 2：已完成
    AGVTaskStatusI.agvId=ReplyJson.value("agvId").toInt();          //agv 编号
    AGVTaskStatusI.station=ReplyJson.value("station").toInt();      //动作站点
    AGVTaskStatusI.taskType=ReplyJson.value("taskType").toInt();    //0:装载（取货）1:卸载（送货）
    AGVTaskStatusI.msg=ReplyJson.value("msg").toString();           //任务反馈描述
    if(AGVTaskStatusI.taskId!="" && AGVTaskStatusI.agvId>0){
         ReplyJsonI.status=0;
         if(AGVTaskStatusMap.contains(QString::number(AGVTaskStatusI.agvId))){
             AGVTaskStatusMap.find(QString::number(AGVTaskStatusI.agvId)).value()=AGVTaskStatusI;
         }else{
            AGVTaskStatusMap.insert(QString::number(AGVTaskStatusI.agvId),AGVTaskStatusI);
         }
    }else{
        ReplyJsonI.status=-1;
    }
    //ReplyJson_taskStatus=ReplyJson;
    qDebug()<<"agvTaskStatus--->taskId:"<<AGVTaskStatusI.taskId<<" agvId:"<<AGVTaskStatusI.agvId<<" status:"<<AGVTaskStatusI.status
           <<" ReplyJson:"<<ReplyJson;

    if(ESS_Single::GetInstance()->_language == "VI")
    {
//        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->_errorMsgMap_VI.value(ReplyJsonI.status);
    }else
    {
        ReplyJsonI.errorMsg=ESS_Single::GetInstance()->_errorMsgMap.value(ReplyJsonI.status);
    }

    return ReplyJsonI;
}


//http 接口数据解析返回
QJsonObject HttpServer::_ReplyJson(QString action, ReplyJson ReplyJsonI)
{
    QJsonObject ReJson;
    QJsonObject tasksJson;
    if(currentReplyJsonI.taskType!="taskStatus" && !currentReplyJsonI.taskType.contains("newTask") && currentReplyJsonI.ReplyJson.isEmpty()){
        ReJson.insert("retcode",-2);        //0 代表正常，其余代表异常
        ReJson.insert("message","JOSN 解析为空");
    }else{
        if(currentReplyJsonI.taskType=="taskStatus" || currentReplyJsonI.taskType=="device"
                || currentReplyJsonI.taskType=="statusAgv" || currentReplyJsonI.taskType=="deviceStatus"
                || currentReplyJsonI.taskType.contains("StationStatus?lang=")){
            return currentReplyJsonI.tasksJson;
        }else{
            ReJson.insert("retcode",ReplyJsonI.status);        //0 代表正常，其余代表异常
            ReJson.insert("message",ReplyJsonI.errorMsg);
            QJsonDocument document=QJsonDocument(ReJson);
            QByteArray ReplyData = document.toJson();           // 转换成QByteArray
        }
    }

    if(currentReplyJsonI.taskType!="agvStatus" && currentReplyJsonI.taskType!="agvTaskStatus"){
        qDebug()<<currentReplyJsonI.taskType<<"_ReplyJson: "<<action<<" error:"<<ReplyJsonI.status<<ReplyJsonI.errorMsg<<" ReJson:"<<ReJson;
    }
    ReplyJson ReplyJsonI_;
    currentReplyJsonI=ReplyJsonI_;
    return  ReJson;

}

ReplyJson HttpServer::checkTaskRepeat(SAPExcelInfo SAPExcelInfoI,ReplyJson ReplyJsonI)
{
    bool move_s=false;bool move_e=false;
    QMap<QString, shelfBinInfo> shelfBinInfoMap= ESS_Single::GetInstance()->getshelfBinInfoMap("","","");
    QMap<QString ,shelfBinInfo>::const_iterator iter_=shelfBinInfoMap.begin();
    while (iter_!=shelfBinInfoMap.end()) {
        if(iter_!=nullptr){

        }else{
            return  ReplyJsonI;
        }
        iter_++;
    }
    return ReplyJsonI;
}

ReplyJson HttpServer::checkTaskRepeat_run(SAPExcelInfo SAPExcelInfoI,ReplyJson ReplyJsonI)
{
    ReplyJsonI.status=4;// 任务无法放行
    QMap<int, SAPExcelInfo> CurrentSAPExcelInfoTaskMap= ESS_Single::GetInstance()->getCurrentSAPExcelInfoTask();
    QMap<int, SAPExcelInfo>::const_iterator iter_=CurrentSAPExcelInfoTaskMap.begin();
    while (iter_!=CurrentSAPExcelInfoTaskMap.end()) {
        if(iter_!=nullptr){
            if(iter_.value().taskId==SAPExcelInfoI.taskId
                    && (iter_.value().taskStatus==taskTake_wait || iter_.value().taskStatus==taskTake_wait2)){
                ReplyJsonI.status=0;
            }
        }else{
            return  ReplyJsonI;
        }
        iter_++;
    }
    return ReplyJsonI;
}

void HttpServer::ProcesskeyId_agv()
{
    if(keyId_agv==""){
           QJsonObject tasksJson;
           keyId_agv=QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
           agvTimer=QDateTime::currentDateTime();
           _agvStaustTask->setWebService_Get(keyId_agv, ServerInitItem.sapPathMap.value(1010).Path, "", ServerInitItem.sapPathMap.value(1010).Type);
       }else{
           ReplyMsg ReplyMsgI = _agvStaustTask->GetMsgRecv_Mes(keyId_agv);
           if(ReplyMsgI.keyId==keyId_agv){
               keyId_agv="";
               QJsonObject ReplyJson=ReplyMsgI.ReplyJson;
               QJsonObject ReJson;
               QJsonArray rootFruitList=ReplyJson.value("agvList").toArray();
               QJsonValue value = ReplyJson.value("agvList");//第一级对象 的值
               if (value.isObject()){  //判读第二级对象是否存在
                   ReJson=value.toObject();
                   qDebug() <<"ReJson agvList:"<<ReJson;
               }if(value.isArray()){//判读第二级对象是以数组形式存在
                   for(int i=0;i<rootFruitList.size();i++){
                       ReJson=value[i].toObject();
                       AGVStatus AGVStatusI;
                       AGVStatusI.agvId=ReJson.value("agvId").toInt();                         //agv 编号
                       AGVStatusI.taskId=ReJson.value("taskId").toString();                    //任务 ID，未执行任务可为空
                       AGVStatusI.unitId=ReJson.value("unitId").toString();                    //子任务 ID，未执行任务可为空
                       AGVStatusI.state=ReJson.value("state").toInt();                         //AGV 状态 0：空闲，1：正在运行， 2：异常，3：正在充电，-1:掉线，-2:未重定位
                       AGVStatusI.errorCode=ReJson.value("errorCode").toInt();                 //故障代码（故障代码参考表）
                       AGVStatusI.alarmCode=ReJson.value("alarmCode").toInt();                 //警告代码（警告代码参考表）
                       AGVStatusI.batteryLevel=ReJson.value("batteryLevel").toInt();           //AGV 电量，百分比
                       AGVStatusI.currentNode=ReJson.value("mCurrentNodeID").toInt();          //当前的节点
                       AGVStatusI.lastNodeId=ReJson.value("lastNodeId").toInt();               //上次的节点
                       AGVStatusI.currentStation=ReJson.value("currentStation").toInt();       //当前的站点
                       AGVStatusI.lastNodeStation=ReJson.value("lastNodeStation").toInt();     //上次节点所属的站点
                       AGVStatusI.agvPosition=ReJson.value("agvPosition").toString();          //AGV位置信息(x,y)---(6090,20310)

                       QVariant inputIOStatus = ReJson.value("inputIOStatus");
                       AGVStatusI.inputIOStatus=inputIOStatus.toLongLong();                     //输入IO状态（64个状态位）

                       QVariant outputIOStatus = ReJson.value("outputIOStatus");
                       AGVStatusI.outputIOStatus=outputIOStatus.toLongLong();                   //输出IO状态（64个状态位）

                       AGVStatusI.bottomQRCode=ReJson.value("bottomQRCode").toInt();           //底部读取到的二维码 0 未读取到
                       AGVStatusI.topQRCode=ReJson.value("topQRCode").toInt();                 //顶部读取到的二维码 0 未读取到
                       AGVStatusI.liftHeight=ReJson.value("liftHeight").toInt();               //仓储型小车顶盘高度（mm）-1 代表未初始化
                       AGVStatusI.liftAngle=ReJson.value("liftAngle").toInt();                 //仓储型小车顶盘角度（°）-1 代表未初始化
                       AGVStatusI.mCurrentTaskNum=ReJson.value("mCurrentTaskNum").toInt();     //小车本地任务数量，不为 0 小车在动作                 

                       if(AGVStatusI.agvId>0){
                           if(AGVStatusMap.contains(QString::number(AGVStatusI.agvId))){
                               AGVStatusMap.find(QString::number(AGVStatusI.agvId)).value()=AGVStatusI;
                           }else{
                               AGVStatusMap.insert(QString::number(AGVStatusI.agvId),AGVStatusI);
                           }
                       }
                   }

                   onLineTimer=QDateTime::currentDateTime();

                   emit sig_AGVStatusWidget(AGVStatusMap,AGVTaskStatusMap);

                   HttpOnLineTimer=QDateTime::currentDateTime();//启动端口 20001 超时计时
               }
               ReplyJson_agvStatus=ReplyJson;
               //qDebug()<<"RCS 返回AGV状态信息--->ReplyJson_agvStatus:"<<ReplyJson;
           }

           qint64 onLinecount = agvTimer.secsTo(QDateTime::currentDateTime());
           if(onLinecount>20){
               keyId_agv="";
           }
    }
}

void HttpServer::UpdateStation(QString Station, int stationStatus, QString scanCode)
{

    //临时库位状态信息容器
    QMap<QString,deviceI>currentStationInfoMapI;

    //临时库位状态参数集合
    deviceI currentStationI;

    currentStationI = currentStationInfoMap.value(Station);
    currentStationI.stationStatus = stationStatus;          //站点状态 0: 无料车 1：有任务 2：有料车 3:已人工处理
    currentStationI.scanCode = scanCode;                    //更新工单条码
    currentStationInfoMapI.insert(Station, currentStationI);
    threadQueryI->UP_deviceI(currentStationInfoMapI);       //更新库位状态信息
}

QString HttpServer::GetStation(QString Station, int stationStatus, QString scanCode)
{

    QString StationI = "";

    //电镀空料暂存库位
    if(Station == "DDK" && scanCode.isEmpty())
    {
        if(currentStationInfoMap.value("DDK1").stationStatus == stationStatus){
            StationI = "DDK1";
        }else if(currentStationInfoMap.value("DDK2").stationStatus == stationStatus){
            StationI = "DDK2";
        }else if(currentStationInfoMap.value("DDK3").stationStatus == stationStatus){
            StationI = "DDK3";
        }else if(currentStationInfoMap.value("DDK4").stationStatus == stationStatus){
            StationI = "DDK4";
        }else if(currentStationInfoMap.value("DDK5").stationStatus == stationStatus){
            StationI = "DDK5";
        }else if(currentStationInfoMap.value("DDK6").stationStatus == stationStatus){
            StationI = "DDK6";
        }else if(currentStationInfoMap.value("DDK7").stationStatus == stationStatus){
            StationI = "DDK7";
        }
    }

    //电镀满料暂存库位
    if(Station == "DDM" && scanCode.isEmpty())
    {
        if(currentStationInfoMap.value("DDM1").stationStatus == stationStatus){
            StationI = "DDM1";
        }else if(currentStationInfoMap.value("DDM2").stationStatus == stationStatus){
            StationI = "DDM2";
        }else if(currentStationInfoMap.value("DDM3").stationStatus == stationStatus){
            StationI = "DDM3";
        }else if(currentStationInfoMap.value("DDM4").stationStatus == stationStatus){
            StationI = "DDM4";
        }else if(currentStationInfoMap.value("DDM5").stationStatus == stationStatus){
            StationI = "DDM5";
        }else if(currentStationInfoMap.value("DDM6").stationStatus == stationStatus){
            StationI = "DDM6";
        }else if(currentStationInfoMap.value("DDM7").stationStatus == stationStatus){
            StationI = "DDM7";
        }else if(currentStationInfoMap.value("DDM8").stationStatus == stationStatus){
            StationI = "DDM8";
        }
    }

    //电镀满料检测库位
    if(Station == "DDMJ" && scanCode.isEmpty())
    {
        if(currentStationInfoMap.value("DDMJ1").stationStatus == stationStatus){
            StationI = "DDMJ1";
        }else if(currentStationInfoMap.value("DDMJ2").stationStatus == stationStatus){
            StationI = "DDMJ2";
        }else if(currentStationInfoMap.value("DDMJ3").stationStatus == stationStatus){
            StationI = "DDMJ3";
        }
    }

    //外层空料暂存库位
    if(Station == "WCK" && scanCode.isEmpty())
    {
        if(currentStationInfoMap.value("WCK1").stationStatus == stationStatus){
            StationI = "WCK1";
        }else if(currentStationInfoMap.value("WCK2").stationStatus == stationStatus){
            StationI = "WCK2";
        }else if(currentStationInfoMap.value("WCK3").stationStatus == stationStatus){
            StationI = "WCK3";
        }
    }

    //外层满料暂存库位
    if(Station == "WCM" && scanCode.isEmpty())
    {
        if(currentStationInfoMap.value("WCM1").stationStatus == stationStatus){
            StationI = "WCM1";
        }else if(currentStationInfoMap.value("WCM2").stationStatus == stationStatus){
            StationI = "WCM2";
        }else if(currentStationInfoMap.value("WCM3").stationStatus == stationStatus){
            StationI = "WCM3";
        }else if(currentStationInfoMap.value("WCM4").stationStatus == stationStatus){
            StationI = "WCM4";
        }else if(currentStationInfoMap.value("WCM5").stationStatus == stationStatus){
            StationI = "WCM5";
        }else if(currentStationInfoMap.value("WCM6").stationStatus == stationStatus){
            StationI = "WCM6";
        }else if(currentStationInfoMap.value("WCM7").stationStatus == stationStatus){
            StationI = "WCM7";
        }else if(currentStationInfoMap.value("WCM8").stationStatus == stationStatus){
            StationI = "WCM8";
        }else if(currentStationInfoMap.value("WCM9").stationStatus == stationStatus){
            StationI = "WCM9";
        }
    }

    //外层已烤库位
    if(Station == "WCYK" && !scanCode.isEmpty())
    {
        if(currentStationInfoMap.value("WCYK1").stationStatus == stationStatus
                && currentStationInfoMap.value("WCYK1").scanCode == scanCode){
            StationI = "WCYK1";
        }else if(currentStationInfoMap.value("WCYK2").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK2").scanCode == scanCode){
            StationI = "WCYK2";
        }else if(currentStationInfoMap.value("WCYK3").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK3").scanCode == scanCode){
            StationI = "WCYK3";
        }else if(currentStationInfoMap.value("WCYK4").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK4").scanCode == scanCode){
            StationI = "WCYK4";
        }else if(currentStationInfoMap.value("WCYK5").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK5").scanCode == scanCode){
            StationI = "WCYK5";
        }else if(currentStationInfoMap.value("WCYK6").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK6").scanCode == scanCode){
            StationI = "WCYK6";
        }else if(currentStationInfoMap.value("WCYK7").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK7").scanCode == scanCode){
            StationI = "WCYK7";
        }else if(currentStationInfoMap.value("WCYK8").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK8").scanCode == scanCode){
            StationI = "WCYK8";
        }else if(currentStationInfoMap.value("WCYK9").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK9").scanCode == scanCode){
            StationI = "WCYK9";
        }else if(currentStationInfoMap.value("WCYK10").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK10").scanCode == scanCode){
            StationI = "WCYK10";
        }else if(currentStationInfoMap.value("WCYK11").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK11").scanCode == scanCode){
            StationI = "WCYK11";
        }else if(currentStationInfoMap.value("WCYK12").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK12").scanCode == scanCode){
            StationI = "WCYK12";
        }else if(currentStationInfoMap.value("WCYK13").stationStatus == stationStatus
                 && currentStationInfoMap.value("WCYK13").scanCode == scanCode){
            StationI = "WCYK13";
        }
    }

    return StationI;
}

void HttpServer::Map_write()
{
    //库位二维码容器
    _StationQRcodeMap.insert("DD1", "00000016");
    _StationQRcodeMap.insert("DD2", "00000027");

    _StationQRcodeMap.insert("DDMJ1", "00000017");
    _StationQRcodeMap.insert("DDMJ2", "00000019");
    _StationQRcodeMap.insert("DDMJ3", "00000020");

    _StationQRcodeMap.insert("DDM1", "00000036");
    _StationQRcodeMap.insert("DDM2", "00000037");
    _StationQRcodeMap.insert("DDM3", "00000038");
    _StationQRcodeMap.insert("DDM4", "00000039");
    _StationQRcodeMap.insert("DDM5", "00000040");
    _StationQRcodeMap.insert("DDM6", "00000041");
    _StationQRcodeMap.insert("DDM7", "00000042");
    _StationQRcodeMap.insert("DDM8", "00000043");

    _StationQRcodeMap.insert("WC1", "00000028");
    _StationQRcodeMap.insert("WC2", "00000014");

    _StationQRcodeMap.insert("WCK1", "00000032");
    _StationQRcodeMap.insert("WCK2", "00000031");
    _StationQRcodeMap.insert("WCK3", "00000030");

    _StationQRcodeMap.insert("WCYK1", "00000035");
    _StationQRcodeMap.insert("WCYK2", "00000034");
    _StationQRcodeMap.insert("WCYK3", "00000033");
    _StationQRcodeMap.insert("WCYK4", "00000001");
    _StationQRcodeMap.insert("WCYK5", "00000002");
    _StationQRcodeMap.insert("WCYK6", "00000003");
    _StationQRcodeMap.insert("WCYK7", "00000004");
    _StationQRcodeMap.insert("WCYK8", "00000005");
    _StationQRcodeMap.insert("WCYK9", "00000006");
    _StationQRcodeMap.insert("WCYK10", "00000007");
    _StationQRcodeMap.insert("WCYK11", "00000008");
    _StationQRcodeMap.insert("WCYK12", "00000009");
    _StationQRcodeMap.insert("WCYK13", "00000010");

    _StationQRcodeMap.insert("WCM1", "00000152");
    _StationQRcodeMap.insert("WCM2", "00000151");
    _StationQRcodeMap.insert("WCM3", "00000046");
    _StationQRcodeMap.insert("WCM4", "00000047");
    _StationQRcodeMap.insert("WCM5", "00000048");
    _StationQRcodeMap.insert("WCM6", "00000049");
    _StationQRcodeMap.insert("WCM7", "00000050");
    _StationQRcodeMap.insert("WCM8", "00000045");
    _StationQRcodeMap.insert("WCM9", "00000044");

}

